package pe.com.trener.layer.model.persistence.daoimpl;

/**
 * @author:   Oscar Erick Candela Carbajal
 * @rol:	  Analista Programador Web Java
 * @proyecto: Portal Trener
 *
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.struts.util.LabelValueBean;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pe.com.trener.layer.model.persistence.dao.SemanaDao;
import pe.com.trener.layer.model.pojo.Semana;

public class SemanaDaoImpl extends HibernateDaoSupport implements SemanaDao {

	@SuppressWarnings("unchecked")
	public List<Semana> buscarTodos() throws Exception {
		try {   
			List<Semana> resultQuery = new ArrayList<Semana>();
			List resultLabelValue = new ArrayList();
			HibernateTemplate ht = getHibernateTemplate();
		 		        
	        String queryString = " from " +
								 " Semana o ";
	        
	        resultQuery = (List<Semana>)ht.find(queryString);
	        	        
	        Iterator<Semana> it = resultQuery.iterator();
	        
	        while (it.hasNext()) {
	        	Semana objectModel = (Semana)it.next();
	        	LabelValueBean objectLabelValueBean = new LabelValueBean();
	        	objectLabelValueBean.setLabel(objectModel.getCodigoSemana().toString());
	        	objectLabelValueBean.setValue(objectModel.getCodigoSemana().toString());
	        	resultLabelValue.add(objectLabelValueBean);
	        }
	        return resultLabelValue;
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {		
		}        
	}
	
	@SuppressWarnings("unchecked")
	public List<Semana> buscarDisponible() throws Exception {
		try {   
			List<Semana> resultQuery = new ArrayList<Semana>();
			List resultLabelValue = new ArrayList();
			HibernateTemplate ht = getHibernateTemplate();
		 		        
	        String queryString = " from " +
								 " Semana o " +
								 " where " +
								 " o.codigoSemana not in (select c.semana.codigoSemana from ClassBook c) ";
	        
	        resultQuery = (List<Semana>)ht.find(queryString);
	        	        
	        Iterator<Semana> it = resultQuery.iterator();
	        
	        while (it.hasNext()) {
	        	Semana objectModel = (Semana)it.next();
	        	LabelValueBean objectLabelValueBean = new LabelValueBean();
	        	objectLabelValueBean.setLabel(objectModel.getCodigoSemana().toString());
	        	objectLabelValueBean.setValue(objectModel.getCodigoSemana().toString());
	        	resultLabelValue.add(objectLabelValueBean);
	        }
	        return resultLabelValue;
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {		
		}        
	}	
	
}